<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!doctype html>
<html>
<head>
<title>${type=='save'?'添加':'修改' }角色</title>
<%@ include file="../../../common/base.jsp" %>
</head>

<body id="body">
<!--head-->
<div id="head">
	<%@ include file="../../../common/head.jsp" %>
</div>
<!--head end-->
<!--container-->
<div id="container">
	<div id="pageId" style="display: none;">04-01-01</div>
		<!--sidebar-->
		<%@ include file="../../../common/menu.jsp" %>
	    <!--sidebar-->
    <!--main-->
    <div class="main">
    	<div class="location" style="background-position:22px -109px;">
        	系统管理<span>&gt;</span>角色管理<span>&gt;</span>${type=='save'?'添加':'修改' }角色
        </div>
        <!--content-->
        <div class="content">
            <div class="info_container pd_t_10">
                <label>角色名称：<input type="text" onFocus="this.style.border = '1px solid #2A7CCF'"  class="text" id="roleName" value="${roleName}" onBlur="roleNameValide(this);this.style.border = '1px solid #D5D5D5'"/></label>
                <button type="button" class="btn_save" onClick="save();">保存</button>
            </div>
            <!--渠道列表-->
            <div id="tableResult">
            	<div class="listBox">
	            	<h3>渠道列表：</h3>
	                <ul id="roleList">
	                	<li>
	                    	<input type="checkbox" class="cbAll" onClick="switchCb();"><span>全选/全不选</span>
	                    </li>
	                </ul>
	            </div>
	            <!--渠道列表-->
	            <!--权限列表-->
	            <div class="listBox">
	            	<h3>权限列表：<button type="button" class="btn" onClick="spreadAll();" id="spreadAll">全部展开</button></h3>
	                <ul id="rightTree">
	                	<li>
	                    	<input type="checkbox" id="selectAll" onClick="selectCheckBox();"><span>全选/全不选</span>
	                    </li>
	                </ul>
	            </div>
	            <!--权限列表-->
            </div>
            
        </div>
        <!--content-->
        <!--foot-->
        <div id="foot">
        	<p>北京掌中彩信息科技有限公司  提供技术支持</p>
        </div>
        <!--foot-->
    </div>
    <!--main-->
</div>
<!--container-->
<script src="${pageContext.request.contextPath}/static/js/basic.js"></script>
<script type="text/javascript">

var TOP_MENUID = "100000000";

$(document).ready(function(e) {
	$("#rightTree li").attr("id",TOP_MENUID);
	init();
});
//初始化加载渠道列表、权限树相关数据
function init(){
    //alert('aaw3');
	$("#tableResult").showLoading();
	$.ajax({
		   type: "POST",
		   url: "${pageContext.request.contextPath}/roleManagement/queryTree",
		   dataType:"json",
		   data:{
		   		type:"${type}",
		   		roleId:"${roleId}"
		   },
		   success: function(data){
		   		//var partnerData = data.partnerData;
		   		//var chData = data.chData;
		   		var rightData = data.rightData;
		   		//绑定渠道列表数据
		   		//console.log(JSON.stringify(partnerData));
		   		/*if(partnerData != null && partnerData.length > 0){
			   		for (var i = 0; i < partnerData.length; i++) {
			   			var partnerId = partnerData[i].PARTNER_ID;
						$("#roleList").append("<li><input name='rolePartner' type='checkbox' class='cb' onclick='reCb(this);' value='"+partnerData[i].PARTNER_ID	
							+"' "+partnerData[i].CHECKED+"><span>"+partnerData[i].NAME+"</span></li>");
						for (var j = 0; j < chData.length; j++) {	
							if(partnerId == chData[j].PARENT_PARTNERID){
								$("#roleList").append("<li style='margin-left: 20px;'><input name='rolePartner' type='checkbox' class='cb' onclick='reCb(this);' value='"+chData[j].PARTNER_ID	
									+"' "+chData[j].CHECKED+"><span>"+chData[j].NAME+"</span></li>");
							}
						}
					}
		   		}else{
		   			for (var i = 0; i < chData.length; i++) {
						$("#roleList").append("<li><input name='rolePartner' type='checkbox' class='cb' onclick='reCb(this);' value='"+chData[i].PARTNER_ID	
							+"' "+chData[i].CHECKED+"><span>"+chData[i].NAME+"</span></li>");
					}
		   		}*/
				//绑定权限树数据
		   		//console.log(JSON.stringify(rightData));
				for (var j = 0; j < rightData.length; j++) {
					createMenuTree(rightData[j]);
				}
				if ("${type}"=="update") {
					initRoleCb();
					initRightCb();
				}
				$("#tableResult").hideLoading();
		   }
	});
}

var i;
function roleNameValide(e){
	i = $(e).parent().parent().find("i");
	if (i.length==0) {
		$(e).parent().parent().append("<i/>");
		i = $(e).parent().parent().find("i");
	}
	var flag = _reg(e, /^[\u4E00-\u9FA5A-Za-z0-9]{5,22}$/, "输入错误，由5-22位数字、字母或汉字组成！", false,i,null);
	if (!flag) {
		i = $(e).parent().parent().find("i");
		i.attr("class","zhu_shi_3");
	}
	return flag;
}

//保存角色
function save(){
	var partnerIds = "";
	var menuIds = "";
	var type = "${type}";
	var roleId = "${roleId}";
	var roleName = $("#roleName").val();
	$("#roleList input").each(function(i,e){
		if ($(e).attr("checked") && $(e).attr("value")!="on") {
			partnerIds += $(e).attr("value")+",";
		}
	});
	$("#rightTree input").each(function(i,e){
		if ($(e).attr("checked") && $(e).attr("value")!="on") {
			menuIds += $(e).attr("value")+",";
		}
	});
	if (!roleNameValide($("#roleName"))) {
		return;
	} /*
	if (partnerIds=='') {
		i.attr("class","zhu_shi_3");
		i.text("请至少选择一个渠道！");
		return;
	}*/
	if (menuIds=='') {
		i.attr("class","zhu_shi_3");
		i.text("请至少选择一个功能权限！");
		return;
	}
	$.ajax({
		   type: "POST",
		   url: "${pageContext.request.contextPath}/roleManagement/saveOrUpdateRole",
		   dataType:"json",
		   data:{
		   		type:type,
		   		roleId:roleId,
		   		roleName:roleName,
				partnerIds:partnerIds,
				menuIds:menuIds
		   },
		   success: function(data){
		   		if ("角色已存在!"==data.msg) {
		   			i.attr("class","zhu_shi_3");
					i.text(data.msg);
					return;
				}
		   		prompt.alert(data.msg,function(){
		   			if (data.errorCode==0) {
						window.location.href="${pageContext.request.contextPath}/roleManagement/showRoleManager";
					}
		   		});
		   }
	});
}

//权限树展开所有
function spreadAll(){
	
	if ($("#spreadAll").text()=="全部展开") {
		$("#rightTree ul").show();
		$("#spreadAll").text("全部收缩");
	} else {
		$("#rightTree ul").hide();
		$("#spreadAll").text("全部展开");
	}
}
//权限树单个展开收缩
function spread(clsName){
	$("."+clsName).each(function(i,e){
		if ($(e).is(":hidden")) {
			$(e).show();
		} else {
			$(e).hide();
		}
	});
}
//修改时回写渠道列表中checkbox后，全选按钮的处理
function initRoleCb(){
	var flag = true;
	$(".cb").each(function(i,e){
		if (!$(e).attr("checked")) {
			flag = false;
			return flag;
		}
	});
	if (flag) {
		$(".cbAll").attr("checked",true);
	}
}

//修改时回写权限树中checkbox后父级节点的处理
function initRightCb(){
	var flag = false;
	$("#rightTree input").each(function(i,e){
		if (flag=$(e).attr("checked")) {
			var menuId = $(e).attr("value");
			var parentMenuId = $("#"+menuId).attr("value");
			dealParent(parentMenuId, flag);
		}
	});
}

//权限树checkbox全选
function selectCheckBox(){
	if ($("#selectAll").attr("checked")) {
		$("#rightTree input").attr("checked",true);
	} else {
		$("#rightTree input").attr("checked",false);
	}
}

//权限树单个checkbox处理方法
function selectCb(menuId,parentMenuId){
	var flag = false;
	//子节点处理
	$("."+menuId+" input").each(function(i,e){//查询子元素下所有checkbox
		if ($("#"+menuId+" input:eq(0)").attr("checked")) {//当前checkbox
			$(e).attr("checked",true);
		} else {
			$(e).attr("checked",false);
		}
	});
	
	if ($("#"+menuId+" input:eq(0)").attr("checked")) {
		flag = true;
	}
	dealParent(parentMenuId,flag);
}

//权限树checkbox父级节点相关处理
function dealParent(parentMenuId,flag){
	var run = true;
	var mark = true;
	if ($("#"+parentMenuId+" input:eq(0)").length>0) {//检查是否有父节点
		if (!flag) {
			$("."+parentMenuId+" input").each(function(i,e){//检查同级元素是否有选中的
				if ($(e).attr("checked")) {
					run = false;
					return run;
				}
			});
		}else {
			$("#rightTree input").each(function(i,e){//查询是否全部选中
				if (!$(e).attr("checked") && $(e).attr("id")!="selectAll") {
					mark = false;
					return mark;
				}
			});
		}
		if (run) {
			$("#"+parentMenuId+" input:eq(0)").attr("checked",flag);
			if (!mark) {
				$("#selectAll").attr("checked",mark);//取消全选按钮√
			}
			dealParent($("#"+parentMenuId).attr("value"),flag);
		}else {
			$("#selectAll").attr("checked",run);//取消全选按钮√
		}
	}
}

//创建权限树
function createMenuTree(obj){
	var li = "<li id="+obj.MENU_ID+" value="+obj.PARENT_MENU_ID+"><input name='roleMenu' type='checkbox' value='"+obj.MENU_ID+"'" +obj.CHECKED+" onclick='selectCb("+obj.MENU_ID+","+obj.PARENT_MENU_ID+");'><span style='cursor:pointer;' onclick='spread("+obj.MENU_ID+");'>"+obj.MENU_NAME+"</span></li>";
	if (obj.PARENT_MENU_ID==TOP_MENUID) {//一级菜单
		$("#rightTree").append(li);
		$("#"+obj.MENU_ID+" input").attr("class",obj.PARENT_MENU_ID);
	}else {
		$("#"+obj.PARENT_MENU_ID).append("<ul class="+obj.PARENT_MENU_ID+" style='display:none;'>"+li+"</ul>");//找父节点追加元素
	}
	if (obj.SUB_DATA!=undefined) {
		$("#"+obj.MENU_ID+" span").before("<i class='triangle_l'/>");
		for (var i = 0; i < obj.SUB_DATA.length; i++){
			createMenuTree(obj.SUB_DATA[i]);
		}
	}
}

</script>
</body>
</html>